package com.stx.jeepay.merchant.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stx.jeepay.core.entity.PayWayEntity;
import com.stx.jeepay.core.model.ApiPageRes;
import com.stx.jeepay.core.service.MchPayPassageService;
import com.stx.jeepay.core.service.PayOrderService;
import com.stx.jeepay.core.service.PayWayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "支付方式配置")
@RestController
@RequestMapping("api/payWays")
public class PayWayController extends CommonCtrl {

    @Autowired
    PayWayService payWayService;
    @Autowired
    MchPayPassageService mchPayPassageService;
    @Autowired
    PayOrderService payOrderService;

    @ApiOperation("支付方式列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "pageNumber", value = "分页页码", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数（-1时查全部数据）", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "wayCode", value = "支付方式代码"),
            @ApiImplicitParam(name = "wayName", value = "支付方式名称")
    })
    @PreAuthorize("hasAuthority('ENT_PAY_ORDER_SEARCH_PAY_WAY')")
    @GetMapping
    public ApiPageRes<PayWayEntity> list() {
        PayWayEntity queryObject = getObject(PayWayEntity.class);
        LambdaQueryWrapper<PayWayEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(queryObject.getWayCode())) {
            queryWrapper.like(PayWayEntity::getWayCode, queryObject.getWayCode());
        }

        if (StringUtils.isNotEmpty(queryObject.getWayName())) {
            queryWrapper.like(PayWayEntity::getWayName, queryObject.getWayName());
        }
        queryWrapper.orderByDesc(PayWayEntity::getWayCode);

        IPage<PayWayEntity> pages = payWayService.page(getIPage(true), queryWrapper);
        return ApiPageRes.pages(pages);
    }
}
